package com.seesmic.helper
{ 
	
	import com.serialization.json.JSON;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
 	
 	
	[Event(name="ready", type="flash.events.Event")]
	public class VersionManager extends EventDispatcher
	{
		public static const RECORDER:String = "recorder"
		public function VersionManager()
		{
			super();
		}
		
		private static var instance:VersionManager
		public static function getInstance():VersionManager
		{
			if(instance ==null)
			{
				instance = new VersionManager()
			}
			return instance
		}
		private var vloader:URLLoader
		private var versionObject:Object
		private var _versionpath:String = "http://seesmic.com/version.js"
		public var ready:Boolean = false
		public  function getVersion():void
		{
			trace("getting version..")
			vloader = new URLLoader()
			vloader.addEventListener(Event.COMPLETE , onLoadComplete)
			vloader.addEventListener(IOErrorEvent.IO_ERROR, onIOError)
			vloader.load(new URLRequest(_versionpath))
		}
		
		public function getSWF(version:String):String
		{
			return versionObject[version];
		}
		
		private function onLoadComplete(e:Event):void
		{
			versionObject = JSON.deserialize(e.target.data)
			
			_removeEventListener()
			
			this.ready = true;
			
			this.dispatchEvent(new Event('ready'))
		}
		private function onIOError(e:IOErrorEvent):void
		{
			trace("IO Error");
		}
		private function _removeEventListener():void
		{
			vloader.removeEventListener(Event.COMPLETE , onLoadComplete)
			vloader.removeEventListener(IOErrorEvent.IO_ERROR, onIOError)
		}

	}
}